﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Constant.battle;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Formular
{
    class StageFormular
    {
        public static long default_stage_cooling = 1000 * 5;

        public const int default_stage_startindex = 0;

        private static int default_startmap = 101;

        /** 攻击NPC不扣除军工最低等级 */
        private static int protect_actionpoint_attacknpc_level = 35;

        // 10 mins
        private static int default_rve_battletimespan = DateTimeCoder.Instance.MinuteToMilliSecond(1);


        /// <summary>
        /// 能否重复攻击的关卡
        /// </summary>
        /// <param name="stage"></param>
        /// <returns></returns>
        public static bool CanRepeatAttack(Entities.War_Stage stage)
        {
            switch (stage.Stagetypecode)
            {
                case WarStageTypecodeCollection.SPECIAL:
                case WarStageTypecodeCollection.RVE:
                    return true;

                case WarStageTypecodeCollection.OFFICER:
                case WarStageTypecodeCollection.NORMAL:
                default:
                    return false;
            }
        }

        /// <summary>
        /// 获取免费行动力集合
        /// </summary>
        /// <returns></returns>
        public static int createFreeBattleTypeCode()
        {
            return FreeBattleTypecodeCollection.ATTACK_RESOURCE
                + FreeBattleTypecodeCollection.ATTACK_PVE
                + FreeBattleTypecodeCollection.ATTACK_PVP;
        }


        /// <summary>
        /// 是否不消耗军令打资源
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public static bool isFreeAttackResource(long typecode)
        {
            return (typecode & FreeBattleTypecodeCollection.ATTACK_RESOURCE) == FreeBattleTypecodeCollection.ATTACK_RESOURCE;
        }

        /// <summary>
        /// 是否不消耗军令打怪
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public static bool isFreeAttackNpc(long typecode)
        {
            return (typecode & FreeBattleTypecodeCollection.ATTACK_PVE) == FreeBattleTypecodeCollection.ATTACK_PVE;
        }

        /// <summary>
        /// 是否不消耗军令攻击玩家
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public static bool isFreeAttackPlayer(long typecode)
        {
            return (typecode & FreeBattleTypecodeCollection.ATTACK_PVP) == FreeBattleTypecodeCollection.ATTACK_PVP;

        }

        /// <summary>
        /// 获取rve战斗时间
        /// </summary>
        /// <returns></returns>
        public static long GetRveBattleDate()
        {
            return GlobalTimer.Instance.ToTimeSequence() + default_rve_battletimespan;
        }

        /// <summary>
        /// 取得关卡掉落商魂
        /// </summary>
        /// <returns></returns>
        public static long GetStageSoulValue(Entities.War_Stage stage, Entities.Usr_Profile profile)
        {
            if (stage.Stagetypecode == WarStageTypecodeCollection.RVE)
            {
                return GetRveSoulValue(stage, profile, true);
            }
            else
            {
                return GetPveSoulValue(stage, profile, true);
            }
        }

        /// <summary>
        /// 获取关卡军魂
        /// </summary>
        /// <returns></returns>
        public static long GetPveSoulValue(Entities.War_Stage stage, Entities.Usr_Profile profile, bool fightResult)
        {
            //y = 200+8x

            if (!fightResult)
                return 0;

            double soulvalue = 200 + stage.Stagelevel * 8;

            soulvalue = soulvalue * (1 + GetSoulValueFactor(stage.Stagelevel, profile.Userlevel));

            return (long)Math.Round(soulvalue);
        }

        /// <summary>
        /// 关卡产出调整
        /// </summary>
        /// <returns></returns>
        protected static double GetSoulValueFactor(long stagelevel, long userlevel)
        {
            //y= 0.095x+1.05， x = 关卡等级 – 用户等级， y = 影响百分比 Y取值范围是[10%, 200%]

            double x = stagelevel - userlevel;

            double y = 0.095 * x + 1.05;

            if (y < 0.1)
                y = 0.1;

            if (y > 2)
                y = 2;

            return y;

        }

        /// <summary>
        /// 获取战斗军魂奖励
        /// </summary>
        /// <returns></returns>
        public static long GetRveSoulValue(Entities.War_Stage stage, Entities.Usr_Profile profile, bool fightResult)
        {
            if (!fightResult)
                return 0;

            return GetPveSoulValue(stage, profile, fightResult) * 3;
        }

        /// <summary>
        /// 获取rve掉落装备编码，如果没有则返回-1
        /// </summary>
        /// <param name="war_Stage"></param>
        /// <returns></returns>
        public static long GetRveEquipmentSerialcode(Pixysoft.Weblications.Sango.Entities.War_Stage war_Stage)
        {
            return war_Stage.Equipmentserialcode;
        }

        /// <summary>
        /// 是否需要扣除行动力
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="win"></param>
        /// <returns></returns>
        public static bool needDecreaseActionPoint(Pixysoft.Weblications.Sango.Entities.Usr_Profile profile, bool win)
        {
            if (win)
                return true;

            if (profile.Userlevel <= protect_actionpoint_attacknpc_level)
                return false;

            return true;
        }

        /// <summary>
        /// 是否需要冷却队列
        /// </summary>
        /// <param name="warStage"></param>
        /// <returns></returns>
        public static bool needCooldownQueue(Entities.War_Stage warStage)
        {
            if (warStage.Mapserialcode != default_startmap)
                return true;

            return warStage.Stageindex < 2;
        }

        /// <summary>
        ///  是否会掉装备
        /// </summary>
        /// <param name="stage"></param>
        /// <returns></returns>
        public static bool isDropEquipment(Entities.War_Stage stage)
        {
            if (stage.Equipmentserialcode == PlayerFormular.default_number_empty)
                return false;

            Random random = new Random();
            return random.Next(0, 100) < stage.Eqmprobabilityvalue;
        }


    }
}
